package com.lwbldy.modules.system.service.impl;

import com.lwbldy.modules.system.domain.SysRole;
import com.lwbldy.modules.system.domain.SysRoleMenu;
import com.lwbldy.modules.system.domain.SysUserRole;
import com.lwbldy.modules.system.mapper.SysRoleDao;
import com.lwbldy.modules.system.mapper.SysRoleMenuDao;
import com.lwbldy.modules.system.mapper.SysUserRoleDao;
import com.lwbldy.modules.system.service.SysRoleService;
import com.lwbldy.modules.system.vo.MenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    SysRoleDao sysRoleDao;

    @Autowired
    SysRoleMenuDao sysRoleMenuDao;

    @Autowired
    SysUserRoleDao sysUserRoleDao;

    @Override
    public List<SysRole> listAll() {
        return sysRoleDao.queryList(null);
    }

    @Override
    public List<Long>  queryRoleIdList(Long userId) {
        return sysUserRoleDao.queryRoleIdList(userId);
    }

    @Override
    public SysRole queryById(long id) {
        return sysRoleDao.queryObject(id);
    }

    @Override
    public int save(SysRole sysRole) {
        return sysRoleDao.save(sysRole);
    }

    @Override
    public int update(SysRole sysRole) {
        return sysRoleDao.update(sysRole);
    }

    @Override
    public int remove(Long roleId) {
        sysRoleMenuDao.deleteByRoleId(roleId);
        return sysRoleDao.delete(roleId);
    }

    @Override
    public int removeBatch(Long[] ids) {
        return sysRoleDao.deleteBatch(ids);
    }

    @Override
    public int saveRolePower(Long roleId, List<Long> menuIds) {
        //先删除
        sysRoleMenuDao.deleteByRoleId(roleId);
        List<SysRoleMenu> listRole = new ArrayList<>();
        for(Long id : menuIds){
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(id);
            sysRoleMenu.setRoleId(roleId);
            listRole.add(sysRoleMenu);
        }
        return sysRoleMenuDao.saveBatch(listRole);
    }

}
